var express = require("express");
var router = express.Router();

var services = require("../services");

function Result(res, result, userMsg = "请求成功", data = {}) {
  res.send(Object.assign({
    code: 0,
    data: result,
    userMsg
  },data))
}

function verifyToken (req, res) {
  console.log('req.headers.authorization', req.headers.authorization)
  if (!req.headers.authorization) { 
    res.send({
      code: 1000,
      data: '登陆验证过期，请重新登陆',
      userMsg: '登陆验证过期，请重新登陆'
    })
    return false
  }
  return true
} 

// 获取配置
router.get('/getConfig',async function (req,res) {
  if (!verifyToken(req, res)) { return }
  const type = req.query.type;
  const result = await services.getConfig(type);
  Result(res, result)
})

router.post('/updateConfig', async function (req,res) {
  if (!verifyToken(req, res)) { return }
  const params = req.body
  console.log(params)
  const result = await services.updateConfig(params)
  Result(res, result)
})

module.exports = router;